package com.du.fileload.framework;

import com.du.fileload.framework.config.DFileCache;
import com.du.fileload.framework.config.DFileCompressRatio;
import com.du.fileload.framework.config.DFileLoad;
import com.du.fileload.framework.servlet.RegisterFileRequestServlet;
import com.du.fileload.framework.utils.DiskCapacityUtils;
import com.du.fileload.framework.utils.FileUploadUtils;
import com.du.plugin.common.tool.HandleException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.io.File;

@Component
public class DFileLoadFrameWorkInit  implements ApplicationRunner{

    private static final Logger log = LoggerFactory.getLogger(DFileLoadFrameWorkInit.class);

    @Autowired
    private DFileCache fileCache;
    @Autowired
    private DFileLoad fileLoad;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("*********************配置初始化************************");
        log.info("******资源配置拦截："+fileLoad.getRequestPrefix());
        log.info("******资源配置路径："+fileLoad.getConfigPath());
        log.info("******资源加载路径："+fileLoad.getResourcePath());
        log.info("******资源缓存路径："+fileLoad.getResourceTempPath());
        log.info("******资源路径命名："+fileLoad.getResourcePathNameFormat().getName());
        log.info("******资源压缩匹配："+fileLoad.getCompress().getSuffix());
        log.info("******资源压缩级别："+fileLoad.getCompress().getCompressLevle());
        log.info("******资源压缩限制："+ DiskCapacityUtils.getNetFileSizeDescription(fileLoad.getCompress().getLimitLen()));
        log.info("******资源压缩配置：");
        for (DFileCompressRatio item:fileLoad.getCompress().getCompressRatio()) {
            log.info("******  "+ String.format("[ %s  size= %s, quality= %s ]",item.getName(),item.getSize(),item.getQuality()));
        }
        initRourceFolder();
        log.info("*******************配置初始化完成***********************");
    }

    /**
     * 初始化文件资源
     */
    private void initRourceFolder(){
        try {
            File file=new File(fileLoad.getResourcePath());
            if(!file.exists()){
                file.mkdirs();
            }
            file=new File(fileLoad.getResourceTempPath());
            if(!file.exists()){
                file.mkdirs();
            }
            FileUploadUtils.setDefaultBaseDir(fileLoad.getResourcePath());
            FileUploadUtils.setResourcePathNameFormat(fileLoad.getResourcePathNameFormat());
        } catch (Exception e) {
            log.error("资源初始化出错："+e.getMessage());
            HandleException.addEx(e);
        }
    }
    /**
     * 注册文件服务servlet
     * @return
     */
    @Bean
    public ServletRegistrationBean registerRequestServlet() {
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(
                new RegisterFileRequestServlet(), String.format("%s/*",DFileLoad.getInstance().getRequestPrefix()));
        //servletRegistrationBean.addInitParameter("name", "javastack");
        //servletRegistrationBean.addInitParameter("sex", "man");
        return servletRegistrationBean;
    }
}
